﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;

using G.Model;
using GameControls;

namespace GameDesignControls
{
    public partial class BoardControl : UserControl, IDisposable, GameUserControl
    {
        public delegate void GameChangedHandler(GameSpaceDef pGameDef);
        public event GameChangedHandler GameChangedEvent;
        
        private readonly GridPainter _GridPainter;
        private readonly Dictionary<string, Image> _ImageDictionary;
        
        private BoardShapeContainer _ShapeContainer;
        private GameDisplayerDT _GameDisplayerDT;
        private GameSpaceDef _GameDef;
        private int _SquareSize;
        private BoardLinePainter _BoardLinePainter;
        
        public BoardControl()
        {
            InitializeComponent();
            
            _SquareSize = 50;
            _GridPainter = new GridPainter(_SquareSize);
            this.Paint += _GridPainter.OnPaint;
            _ShapeContainer = new BoardShapeContainer(this, _SquareSize);
            this.Paint += _ShapeContainer.OnPaint;
            this.MouseClick += _ShapeContainer.OnMouseClick;
            _ImageDictionary = new Dictionary<string, Image>();

            CreateImageDictionary();
        }

        private void CreateImageDictionary()
        {
            foreach (string key in _ImageList.Images.Keys) 
            {
                Image image = _ImageList.Images[key];
                Image thumb = image.GetThumbnailImage(
                    _SquareSize-4, _SquareSize-4, GetThumbnailImageAbort,
                    System.IntPtr.Zero);
                _ImageDictionary.Add(key, thumb);
            }
        }
        
        bool GetThumbnailImageAbort()
        {
            return false;
        }
        
        public int SquareSize
        {
            get { return _SquareSize; }
            set
            {
                if (_SquareSize != value)
                {
                    _SquareSize = value;
                    CreateImageDictionary();
                    SetupDisplayer();
                    if (GameChangedEvent != null)
                    {
                        GameChangedEvent(_GameDef);
                    }
                }
            }
        }
        
        protected override void Dispose(bool disposing)
        {
            this.Paint -= _GridPainter.OnPaint;
            this.Paint -= _ShapeContainer.OnPaint;
            this.MouseClick -= _ShapeContainer.OnMouseClick;
            
            if (disposing) {
                if (components != null) {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }
        
        public BoardShapeContainer ShapeContainer
        {
            get { return _ShapeContainer; }
        }
        
        public ImageList ImageList
        {
            get { return this._ImageList; }
        }
        
        public Dictionary<string, Image> ImageDictionary
        {
            get { return _ImageDictionary; }
        }
        
        public GameSpaceDef GameDef
        {
            get { return _GameDef; }
            set
            {
                if (_GameDef != value)
                {
                    _GameDef = value;
                    SetupDisplayer();
                    if (GameChangedEvent != null)
                    {
                        GameChangedEvent(value);
                    }
                }
            }
        }
        
        public GameDisplayer GameDisplayer
        {
            get { return _GameDisplayerDT; }
        }

        void SetupDisplayer()
        {
            if (_ShapeContainer == null)
            {
                return;
            }
            
            if (_GameDisplayerDT != null)
            {
                this.Paint -= _ShapeContainer.OnPaint;
                this.MouseClick -= _ShapeContainer.OnMouseClick;
                _ShapeContainer = new BoardShapeContainer(this, _SquareSize);
                this.Paint += _ShapeContainer.OnPaint;
                this.MouseClick += _ShapeContainer.OnMouseClick;
                _BoardLinePainter.Dispose();
                _GameDisplayerDT.Dispose();
            }
            
            if (_GameDef != null)
            {
                _GameDisplayerDT = new GameDisplayerDT(
                    _GameDef, _ShapeContainer, _SquareSize);
                _GameDef.SizeChangedEvent += OnGameSizeChanged;
                _BoardLinePainter = new BoardLinePainter(
                    this, _GameDef, _SquareSize);
            }
        }
        
        private void OnGameSizeChanged(GameSpaceDef pGameDef)
        {
            SetupDisplayer();
        }
        
    }
}
